/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2004
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 * 1307, USA.
 */ 

package br.ufmg.dees.insane.ui.rich.insaneSolver;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;

import br.ufmg.dees.insane.solution.EquilibriumPath;


public class InsaneStepConsoleWriter extends JFrame implements Observer {
	 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Variables declaration - do not modify
	private javax.swing.JButton closeButton;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea jTextArea1;
	// End of variables declaration
	
	/** Creates new form ConsoleWriter */
	public InsaneStepConsoleWriter() {
		initComponents();
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
	private void initComponents() {
		
		this.setTitle("Console Writer");
		Image ico = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Insane64.gif"));
		this.setIconImage(ico);
		this.setSize(new Dimension(630,365));
		this.setMaximumSize(new Dimension(630,365));
		this.setMinimumSize(new Dimension(630,365));
		this.setPreferredSize(new Dimension(630,365));
		this.setResizable(false);
		
		jPanel2 = new javax.swing.JPanel();
		jPanel1 = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		closeButton = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		
		jPanel2.setLayout(null);
		
		getContentPane().setLayout(null);
		
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		jPanel1.setLayout(new java.awt.GridLayout());
		
		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Console"));
		jScrollPane1.setBackground(new java.awt.Color(0, 0, 0));
		jScrollPane1.setForeground(new java.awt.Color(0, 255, 0));
		jTextArea1.setBackground(new java.awt.Color(0, 0, 0));
		jTextArea1.setColumns(20);
		jTextArea1.setForeground(new java.awt.Color(0, 255, 0));
		jTextArea1.setRows(5);
		jScrollPane1.setViewportView(jTextArea1);
		
		jPanel1.add(jScrollPane1);
		
		getContentPane().add(jPanel1);
		jPanel1.setBounds(10, 30, 610, 250);
		
		closeButton.setText("Close");
		closeButton.setFont(new Font("Arial", 0, 14));
		getContentPane().add(closeButton);
		closeButton.setBounds(280, 290, 70, 23);
		closeButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						close(event);
					}
				});
		
		jLabel1.setFont(new java.awt.Font("Arial", 0, 14));
		jLabel1.setForeground(new java.awt.Color(51, 51, 51));
		jLabel1.setText("Insane Console Step Writer");
		getContentPane().add(jLabel1);
		jLabel1.setBounds(420, 10, 190, 14);
		
	}// </editor-fold>
	
	public void close(ActionEvent event){
		this.dispose();
	}
	
//	/**
//	* @param args the command line arguments
//	*/
//	public static void main(String args[]) {
//	java.awt.EventQueue.invokeLater(new Runnable() {
//	public void run() {
//	new InsaneStepConsoleWriter().setVisible(true);
//	}
//	});
//	}
	
	public void update(Observable o, Object arg) {
		DecimalFormat fmt = new DecimalFormat();
		DecimalFormatSymbols fsym = new DecimalFormatSymbols();
		fsym.setDecimalSeparator('.');
		fmt.setDecimalFormatSymbols(fsym);
		fmt.applyPattern("0.000E00");
		DecimalFormat fmt2 = new DecimalFormat();
		fmt2.applyPattern("000");
		
		String s = "\nSTEP-"+String.valueOf(fmt2.format(((EquilibriumPath)arg).getStep().getLabel()))+
		"\tLOAD_FACTOR="+String.valueOf(fmt.format(((EquilibriumPath)arg).getStep().getLoadFactorTotal()))+
		"\tINTERACTION-"+String.valueOf(fmt2.format(((EquilibriumPath)arg).getStep().getCurrentIteration()))+
		"\tERROR="+String.valueOf(fmt.format(((EquilibriumPath)arg).getStep().getCurrentError()));
		
		if(((EquilibriumPath)arg).getStep().getConvergence()) {
			s+="\t***CONVERGENCE***\n";
		}
		
		this.jScrollPane1.getVerticalScrollBar().setValue(jTextArea1.getHeight()+50);
		this.jTextArea1.append(s);
		
	}
	
	
	
	
	
	
//	*****************************************************************************
	
}